package my.springboot.el;


import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

@Configuration
@ComponentScan("my.springboot.el")
@PropertySource("classpath:my/springboot/el/text.properties")
public class ELConfig {
	
	@Value("普通字符串")
	private String normal;
	
	@Value("#{systemProperties['os.name']}")
	private String osName;
	
	@Value("#{T(java.lang.Math).random()*100.0}")
	private Double randomNumber;
	
	@Value("#{demoService.another}")
	private String formAnother;
	
	@Value("classpath:my/springboot/el/text.txt")
	private Resource testFile;
	
	@Value("http://www.baidu.com")
	private Resource testUrl;//以资源的形式注入网址
	
	@Value("${book.name}")//1
	private String bookName;
	
	@Autowired
	private Environment environment;//2
	
	/**
	 * 加上一个Bean才能使用 //1 中的value注入方式
	 * 而 //2 使用Environment的方式获取配置文件中的属性则不需要
	 * 但如果配置文件中的属性是user.name，environment中获取的是当前电脑用户名
	 * 但environment最好还是只用在获取环境信息吧，目的单纯
	 * @return
	 */
	@Bean
	public static PropertySourcesPlaceholderConfigurer PropertyConfigure(){
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	public void outputResource(){
		try {
			System.out.println(normal);
			System.out.println(osName);
			System.out.println(randomNumber);
			System.out.println(formAnother);
			System.out.println(IOUtils.toString(testFile.getInputStream()));
			System.out.println(IOUtils.toString(testUrl.getInputStream()));
			System.out.println(bookName);
			System.out.println(environment.getProperty("book.name"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
